package com.lib.ui.adapter;

import android.content.Context;
import android.view.ViewGroup;

import com.lib.base.R;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;


public abstract class BaseAdapter<T> extends RecyclerView.Adapter<ViewHolder> {

    private static final int VIEW_TYPE = -1;

    private Context context;
    private List<T> mData;

    public BaseAdapter(Context context, List<T> mData) {
        this.context = context;
        this.mData = mData;

    }

    public void updateData(List<T> mData) {
        this.mData = mData;

        notifyDataSetChanged();

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder;
        if (viewType == VIEW_TYPE) {
            holder = ViewHolder.createViewHolder(context, parent, getEmptyLayoutId());
        } else {
            holder = ViewHolder.createViewHolder(context, parent, getLayoutId());
        }

        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        if (getItemViewType(position) == VIEW_TYPE) {

            toBindEmptyViewHolder(holder);

        } else {
            toBindViewHolder(holder, position, mData);

        }
    }

    protected void toBindEmptyViewHolder(ViewHolder holder) {


    }


    @Override
    public int getItemCount() {

        //返回集合的长度
        return mData.size() > 0 ? mData.size() : 1;
    }


    /**
     * 获取条目 View填充的类型
     * 默认返回0
     * 将lists为空返回-1
     *
     * @param position
     * @return
     */
    public int getItemViewType(int position) {
        if (mData.size() <= 0) {
            return VIEW_TYPE;
        }
        return super.getItemViewType(position);
    }

    public abstract int getLayoutId();


    protected abstract void toBindViewHolder(ViewHolder holder, int position, List<T> mData);

    public int getEmptyLayoutId() {

        return R.layout.base_empty;

    }


}